/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.museum.main;

import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.museum.utils.Utils;

public class Jiesuan {
	int minite = 15;
	String user = "chinesegxf";
	String uid = "4209691217";
	String cookie = "MY:SESSION=4e8025b303teliiha73008eb2284e0b3; MY:my_sig_prefix=http%3A%2F%2Fwww.btbird.com%2F; city_id=0; city_old_id=0; MY:UserID=4209691217; MY:SNID=9471d911b04f701f4f3f19151e814da6";
	DefaultHttpClient hc = new DefaultHttpClient();
	String target_price = "300";
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public void begin() throws Exception {
		HttpPost post = Utils.createPost();
		while (true) {
			try {
				System.out.println(df.format(new Date()) + " checking......");
				String url = "http://my.mowhere.com/interface/useritem.php";
				post.setURI(new URI(url));
				post.setHeader("Cookie", cookie);
				post.setEntity(Utils.genUseritemEntity(uid));
				String resjson = Utils.trans(hc.execute(post));
				JsonObject js = new JsonParser().parse(resjson)
						.getAsJsonObject();
				String err = js.get("err").getAsString();
				if ("".equals(err)) {
					List<String> making = new ArrayList<String>();
					JsonObject data = js.getAsJsonObject("data");
					JsonArray done_list = data.getAsJsonArray("processed");
					for (Iterator<JsonElement> i = done_list.iterator(); i
							.hasNext();) {
						JsonElement je = i.next();
						String item_id = je.getAsJsonObject().get("item_id")
								.getAsString();
						harv(post, item_id);
					}
					JsonArray making_list = data.getAsJsonArray("processing");
					for (Iterator<JsonElement> i = making_list.iterator(); i
							.hasNext();) {
						JsonElement je = i.next();
						String item_id = je.getAsJsonObject().get("item_id")
								.getAsString();
						making.add(item_id);
					}
					int making_count = making_list.size();
					JsonArray list = data.getAsJsonArray("list");
					for (Iterator<JsonElement> i = list.iterator(); i.hasNext()
							&& making_count < 5;) {
						JsonObject jso = i.next().getAsJsonObject();
						String item_id = jso.get("item_id").getAsString();
						String item_name = jso.get("item_name").getAsString();
						String description = jso.get("description")
								.getAsString();
						if (!making.contains(item_id)
								&& description.indexOf(target_price + "金币。") > 0) {
							System.out.println(df.format(new Date())
									+ " making:" + item_name);
							make(post, item_id);
							making.add(item_id);
							if (++making_count >= 5)
								break;
						}
					}
				} else {
					System.out.println(df.format(new Date()) + " ERROR:" + err);
				}
				System.out.println(df.format(new Date()) + " check done!");
			} catch (Exception e) {
				e.printStackTrace();
			}
			Thread.sleep(minite * 60 * 1000);
		}
	}

	private void sell(HttpPost post) throws Exception {
		String url = "http://my.mowhere.com/interface/manage_treasure.php";
		post.setURI(new URI(url));
		post.setHeader("Cookie", cookie);
		post.setEntity(Utils.genSellEntity(uid, ""));
		String err = "1";
		while (!"".equals(err)) {
			Thread.sleep(2000);
			String resjson = Utils.trans(hc.execute(post));
			JsonObject js = new JsonParser().parse(resjson).getAsJsonObject();
			err = js.get("err").getAsString();
			JsonObject data = js.getAsJsonObject("data");
			String r = data.get("text").getAsString();
			System.out.println(df.format(new Date()) + " " + r);
		}
	}

	private void harv(HttpPost post, String item_id) throws Exception {
		String url = "http://my.mowhere.com/interface/manage_treasure.php";
		post.setURI(new URI(url));
		post.setHeader("Cookie", cookie);
		post.setEntity(Utils.genHarvEntity(uid, item_id));
		String err = "1";
		while (!"".equals(err)) {
			Thread.sleep(2000);
			String resjson = Utils.trans(hc.execute(post));
			JsonObject js = new JsonParser().parse(resjson).getAsJsonObject();
			err = js.get("err").getAsString();
			JsonObject data = js.getAsJsonObject("data");
			String r = data.get("text").getAsString();
			System.out.println(df.format(new Date()) + " " + r);
			if("储物间容量不足,请先整理储物间.".equals(r)){
				sell(post);
			}
		}
	}

	private void make(HttpPost post, String item_id) throws Exception {
		String url = "http://my.mowhere.com/interface/manage_treasure.php";
		post.setURI(new URI(url));
		post.setHeader("Cookie", cookie);
		post.setEntity(Utils.genMakeEntity(uid, item_id));
		String err = "1";
		while (!("".equals(err) || "正在制作道具！".equals(err))) {
			Thread.sleep(2000);
			String resjson = Utils.trans(hc.execute(post));
			JsonObject js = new JsonParser().parse(resjson).getAsJsonObject();
			err = js.get("err").getAsString();
			JsonObject data = js.getAsJsonObject("data");
			String r = data.get("text").getAsString();
			if (r.startsWith("<div class=\"msg-time\">"))
				System.out.println(df.format(new Date()) + " making start!");
		}
	}

	public static void main(String[] args) throws Exception {
		Jiesuan r = new Jiesuan();
		r.begin();
	}
}
